﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.Security;

using TravelSite.DAL;
using TravelSite.DAL.Repositories;

namespace TravelSite.Code
{
    public class BasePage : Page
    {
        public string JsCodeToRunOnReady { get; set; }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            JsCodeToRunOnReady = "";
        }

        public string BasePath
        {
            get { return ResolveClientUrl("~"); }
        }

        public int ContactId
        {
            get
            {
                return Convert.ToInt32(User.Identity.Name);
            }
        }

        public string ClientPin
        {
            get
            {
                if (User.Identity.IsAuthenticated)
                {
                    using (var repo = RepositoryFactory.ResolveRepository<ApiContactRepository>())
                    {
                        return repo.GetPin(Convert.ToInt32(User.Identity.Name));
                    }
                }
                else
                    return "";
            }
        }

        protected string GetIPAddress()
        {
            return Request.UserHostAddress;
        }

        protected void AlertAndClear(string message)
        {
            Response.Clear();
            Response.Write(string.Format("<script type=\"text/javascript\">alert('{0}');</script>", message));
            Response.End();
        }

        public bool IsIE()
        {
            //return !((Request.Browser.Browser.ToLower() == "chrome") || (Request.Browser.Browser.ToLower() == "firefox"));
            return ((Request.Browser.Browser.ToLower() == "ie") || (Request.Browser.Browser.ToLower() == "mozilla"));
        }

    }
}